/*
 * Copyright (c) 2008-2021 浩瀚深度 All Rights Reserved.
 *
 * <p>FileName: WeChatService.java</p>
 *
 * @author zz
 * @date 2024/4/7
 * @version 1.0
 * History:
 * v1.0.0 2024/4/7 Create
 */
package com.haohan.mess.service;

/**
 * <p>Title: WeChatService</p>
 * <p>Description: </p>
 *
 * @author zz
 */

import com.fasterxml.jackson.core.type.TypeReference;
import com.haohan.mess.api.WeiXinApi;
import com.haohan.mess.base.WxApiException;
import com.haohan.mess.base.WxResultStatus;
import com.haohan.mess.model.BizData;
import com.haohan.mess.model.WxResultBody;
import com.haohan.mess.okhttp.MyCookieStore;
import com.haohan.mess.util.HttpUtils;
import com.haohan.mess.util.JsonUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WeChatService {

    /**
     * 接口地址
     **/
    public static final Map<String, String> URL_MAP = new HashMap<>();

    static {
        //初始化登录cookie
        URL_MAP.put("startLogin", "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=startlogin");

        //二维码图片接口
        URL_MAP.put("getqrcode", "https://mp.weixin.qq.com/cgi-bin/scanloginqrcode");

        //二维码扫描状态接口
        URL_MAP.put("qrcodeAsk", "https://mp.weixin.qq.com/cgi-bin/scanloginqrcode");

        //扫码确认后，登录接口
        URL_MAP.put("bizlogin", "https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login");

        //扫码确认后，登录接口
        URL_MAP.put("searchbiz", "https://mp.weixin.qq.com/cgi-bin/searchbiz");

        //扫码确认后，登录接口
        URL_MAP.put("findListEx", "https://mp.weixin.qq.com/cgi-bin/appmsg");
    }


    public int getQRCodeStatus() {
        WxResultBody askQRCode = WeiXinApi.askQRCode();
        Integer status = askQRCode.getStatus();
        return status;
    }

    /**
     * 搜索公众号
     *
     * @return
     */
    public static WxResultBody<List<BizData>> searchBiz(String keyword) {
        Map<String, String> params = new HashMap<>(10);
        params.put("action", "search_biz");
        params.put("begin", "0");
        params.put("count", "5");
        params.put("query", keyword);
        params.put("token", MyCookieStore.getToken());
        params.put("lang", "zh_CN");
        params.put("f", "json");
        params.put("ajax", "1");

        WxResultBody<List<BizData>> wxResultBody = parseWxResultBody(HttpUtils.doGet(URL_MAP.get("searchbiz"), params),
                new TypeReference<WxResultBody<List<BizData>>>() {
                }
        );

        return wxResultBody;
    }

    /**
     * 转成java bean
     *
     * @param jsonRes json结果字符串
     * @return
     */
    public static <T> T parseWxResultBody(String jsonRes, TypeReference<T> typeReference) {
        T wxResultBody = JsonUtils.jsonToObj(jsonRes, typeReference);
        checkIsSuccess((WxResultBody) wxResultBody);
        return wxResultBody;
    }

    public static void checkIsSuccess(WxResultBody wxResultBody) {
        // 判断是否请求成功
        if (wxResultBody == null) {
            throw new WxApiException(WxResultStatus.FAIL_NULL_RES);
        }

        if (wxResultBody.getBase_resp().getRet() == null
                || wxResultBody.getBase_resp().getRet() != 0) {
            throw new WxApiException(WxResultStatus.FAIL_STATUS);
        }
    }
}


